package com.ysccc.shares.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/import/data")
public class ImportDataController {

	public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
	public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";

	@PostMapping("/excel")
	public ResponseEntity<String> importExcel(@RequestParam("file") MultipartFile file) {
		if (file.isEmpty()) {
			throw new RuntimeException("文件为空，无法解析");
		}
		String suffix = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
		if (OFFICE_EXCEL_2003_POSTFIX.equals(suffix)) {
			
		} else if (OFFICE_EXCEL_2010_POSTFIX.equals(suffix)) {
			
		} else {
			throw new RuntimeException("不支持的文件格式，请上传后缀名为xls和xlsx的文件");
		}
		return new ResponseEntity<String>("success", HttpStatus.OK);
	}
	
	public ResponseEntity<String> clearCache() {
		return new ResponseEntity<String>("success", HttpStatus.OK);
	}
}
